<?php
require_once(realpath(dirname(__FILE__)) . '/Users.php');
require_once(realpath(dirname(__FILE__)) . '/Articles.php');
require_once(realpath(dirname(__FILE__)) . '/Nodes.php');
require_once(realpath(dirname(__FILE__)) . '/Comments.php');

class Database {

	private $db_type;
	private $db_host;
	private $db_name;
	private $db_user;
	private $db_pwd;
	private $db;
	
	public function __construct(){
		$this->db_type = "mysql";
		$this->db_host = "localhost";
		$this->db_name = "bd_renaudmathieu";
		$this->db_user = "root";
		$this->db_pwd = "root";
		$this->db_connect();
	}
	
	public function getDb(){
		return $this->db;
	}
	
	// Permet la connexion a la base de donnees
	public function db_connect() {
		try {
			$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
			$this->db = new PDO($this->db_type.":host=".$this->db_host.";dbname=".$this->db_name, $this->db_user, $this->db_pwd, $pdo_options);
		}catch(PDOException $e) {
			echo $e->getMessage();
			die();
		}
	}
	
	public function db_query($query){
		$this->db_connect();
		$reponse = $this->db->query($query);
		return $reponse;
	}
	
	public function db_exec($exec){
		$this->db_connect();
		$this->db->exec($exec);
	}

	public function db_getUsers(){
		$query = "SELECT * FROM users";
		$statement = $this->db->prepare($query);
		$statement->execute();
		$users = $statement->fetchAll(PDO::FETCH_CLASS, "Users");
		
		// Nodes ecrits
		
		// Comments ecrits
		foreach($users as $user){
			$commentsByAuthor = $this->db_getCommentsByAuthor($user->uid);
			$user_comments = array();
			
			foreach($commentsByAuthor as $comment){
				$user_comments[] = $comment->cid;
			}
			
			$user->comments = $user_comments;
		}
		
		return $users;
	}
	
	public function db_getUserById($uid){
		$query = "SELECT * FROM users WHERE uid=:uid";
		$statement = $this->db->prepare($query);
		$statement->bindValue('uid', $uid);
		$statement->execute();
		$statement->setFetchMode( PDO::FETCH_CLASS, "Users");
		$user = $statement->fetch();
		
		// Nodes ecrits
		
		// Comments ecrits
		
		$commentsByAuthor = $this->db_getCommentsByAuthor($user->uid);
		$user_comments = array();
			
		foreach($commentsByAuthor as $comment){
			$user_comments[] = $comment->cid;
		}
			
		$user->comments = $user_comments;
		
		
		return $user;
	}
	
	public function db_getUser($login, $password){
		$query = "SELECT * FROM users WHERE login LIKE :login AND password LIKE :password";
		$statement = $this->db->prepare($query);
		$statement->bindValue('login', $login);
		$statement->bindValue('password', $password);
		$statement->execute();
		$statement->setFetchMode( PDO::FETCH_CLASS, "Users");
		$user = $statement->fetch();
		
		// Nodes ecrits
		
		// Commentaires ecrits
		$commentsByAuthor = $this->db_getCommentsByAuthor($user->uid);
		$user_comments = array();
		foreach($commentsByAuthor as $comment){
			$user_comments[] = $comment->cid;
		}
		$user->comments = $user_comments;
		
		return $user;
	}
	
	public function db_getArticles(){
		$query = "SELECT * FROM articles";
		$statement = $this->db->prepare($query);
		$statement->execute();
		$articles = $statement->fetchAll(PDO::FETCH_CLASS, "Articles");
		
		// Affecter les attributs du Node correspondant
		foreach($articles as $article){
			
			$node = $this->db_getNode($article->aid);
			$article->nid = $node->nid;
			$article->created = $node->created;
			$article->status = $node->status;
			$article->author = $node->author;
			//Comments
			
		}
		
		return $articles;
	}
	
	public function db_getCommentsByNode($nid){
		$query = "SELECT * FROM comments WHERE nid=:nid";
		$statement = $this->db->prepare($query);
		$statement->bindValue('nid', $nid);
		$statement->execute();
		$comments = $statement->fetchAll(PDO::FETCH_CLASS, "Comments");
		return $comments;
	}
	
	public function db_getCommentsByAuthor($author){
		$query = "SELECT * FROM comments WHERE uid=:uid";
		$statement = $this->db->prepare($query);
		$statement->bindValue('uid', $author);
		$statement->execute();
		$comments = $statement->fetchAll(PDO::FETCH_CLASS, "Comments");
		return $comments;
	}
	
	public function db_getNodes(){
		$query = "SELECT * FROM nodes";
		$statement = $this->db->prepare($query);
		$statement->execute();
		$nodes = $statement->fetchAll(PDO::FETCH_CLASS, "Nodes");
		return $nodes;
	}
	
	public function db_getNode($nid){
		$query = "SELECT * FROM nodes WHERE nid=:nid";
		
		$statement = $this->db->prepare($query);
		$statement->bindValue('nid', $nid);
		$statement->execute();
		
		$statement->setFetchMode( PDO::FETCH_CLASS, "Nodes");
		$node = $statement->fetch();
		$statement->closeCursor();
		return $node;
	}
	
	
	public function db_insertNode($author, $type){
		
		$query = "INSERT INTO nodes(created, type, status, author) VALUES(:created, :type, :status, :author)";
		$statement = $this->db->prepare($query);
		$statement->bindValue('created', time());
		$statement->bindValue('type', $type);
		$statement->bindValue('status', 1);
		$statement->bindValue('author', $author);
		$statement->execute();
		$statement->closeCursor();
	}
	
	public function db_insertArticle($data, $author){
		// On cree le node
		$this->db_insertNode($author, "article");
		
		// On recupere le dernier node 
		$nodes = $this->db_getNodes();
		$node = end($nodes);
		
		// On cree l'article
		$query = "INSERT INTO articles(aid, data) VALUES(:aid, :data)";
		$statement = $this->db->prepare($query);
		$statement->bindValue('aid', $node->nid);
		$statement->bindValue('data', $data);
		$statement->execute();
	}
	

	
}

?>